package br.unisinos.arquivos;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.DosFileAttributeView;
import java.nio.file.attribute.DosFileAttributes;
import java.nio.file.attribute.FileOwnerAttributeView;
import java.nio.file.attribute.FileTime;
import java.nio.file.attribute.GroupPrincipal;
import java.nio.file.attribute.PosixFileAttributeView;
import java.nio.file.attribute.PosixFileAttributes;
import java.nio.file.attribute.UserPrincipal;

public class FileAttributeRetriever {
	public int fileKey;
	public long fileSize;
	public long lastModifiedTime;
	public String owner;
	public String group;
	public boolean directory;
	public boolean regularFile;

	public FileAttributeRetriever(Path file) throws IOException {
		init(file);
	}

	private void init(Path file) throws IOException {
		PosixFileAttributeView posixfav = Files.getFileAttributeView(file, PosixFileAttributeView.class);
		if (posixfav != null) {

			PosixFileAttributes posixAttr = posixfav.readAttributes();
			posixInit(posixAttr);

		} else {
			
			dosInit(file);
		}
	}

	private void dosInit(Path file) throws IOException {
		DosFileAttributes dosAttr = Files.getFileAttributeView(file, DosFileAttributeView.class).readAttributes();
		FileOwnerAttributeView ownerAv = Files.getFileAttributeView(file, FileOwnerAttributeView.class);
		if (dosAttr != null) {
			Object fileKey2 = dosAttr.fileKey();
			if (fileKey2 == null) {
				String filePath = file.toString();
				fileKey = filePath.hashCode();
			}else{
				fileKey = fileKey2.hashCode();
			}

			fileSize = dosAttr.size();
			FileTime lastModifiedTime2 = dosAttr.lastModifiedTime();
			if (lastModifiedTime2 != null) {
				lastModifiedTime = lastModifiedTime2.toMillis();
			}
			directory = dosAttr.isDirectory();
			regularFile = dosAttr.isRegularFile();
		}
		if (ownerAv != null) {
			UserPrincipal owner2 = ownerAv.getOwner();
			if (owner2 != null) {
				owner = owner2.getName();
			}
			group = owner;
		}

	}

	private void posixInit(PosixFileAttributes posixAttr) {
		if (posixAttr != null) {

			Object fileKey2 = posixAttr.fileKey();
			if (fileKey2 != null) {
				fileKey = fileKey2.hashCode();
			}
			fileSize = posixAttr.size();
			FileTime lastModifiedTime2 = posixAttr.lastModifiedTime();
			if (lastModifiedTime2 != null) {
				lastModifiedTime = lastModifiedTime2.toMillis();
			}
			directory = posixAttr.isDirectory();
			regularFile = posixAttr.isRegularFile();
			UserPrincipal owner2 = posixAttr.owner();
			if (owner2 != null) {
				owner = owner2.getName();
			}
			GroupPrincipal group2 = posixAttr.group();
			if (group2 != null) {
				group = group2.getName();
			}
		}
	}

	public boolean isRegularFile() {
		return regularFile;
	}

	public boolean isDirectory() {
		return directory;
	}

	public int getFileKey() {
		return fileKey;
	}

	public long getFileSize() {
		return fileSize;
	}

	public long getLastModifiedTime() {
		return lastModifiedTime;
	}

	public String getOwner() {
		return owner;
	}

	public String getGroup() {
		return group;
	}

}
